﻿
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Buttons
{
    public class ButtonGroup:MonoBehaviour
    {
        public Button[] Buttons;
        private Button nowClickButton;//当前点击的按钮

        void Awake()
        {
            foreach (var button in Buttons)
            {
                var button1 = button;//这里声明一个局部变量将button保存下来
                button.onClick.AddListener(() =>
                {
                    if (nowClickButton != null)
                    {
                        nowClickButton.GetComponent<Image>().sprite =
                            nowClickButton.GetComponent<ButtonSprite>().NormalSprite;
                    }
                    button1.GetComponent<Image>().sprite = button1.GetComponent<ButtonSprite>().PressedSprite;
                    button1.transform.SetAsLastSibling();
                    nowClickButton = button1;                    
                });
            }
        }
    }
}